package com.kingschan.util.lang;
import java.security.Key;
import java.security.MessageDigest;
import com.kingschan.util.encryption.Des;
import sun.misc.BASE64Encoder;

/**
 * StringUtil
 * @author kingschan
 *
 */
public class StringUtil {

	/**
	 *getMd5Str
	 * @param targetStr
	 * @return
	 */
	public static String getMd5Str(String targetStr)
	{
		String str = null;
        MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base64en = new BASE64Encoder();
			str=base64en.encode(md5.digest(targetStr.getBytes("utf-8")));
		} catch (Exception e) {			
			e.printStackTrace();
		}
		return str;
        
	}
	
	
	/**
	 * 把null转成 ""
	 * @param obj
	 * @return
	 */
	public static String ConvertNullStr(Object obj){
		if (null==obj) {
			return "";
		}else {
			return obj.toString().trim();
		}
		
	}
	/**
	 * 去掉所有 空格 换行  回车
	 * @param obj
	 * @return
	 */
	public static String replaceSpaceLine(Object obj){
		return ConvertNullStr(obj).replaceAll("\\s*|/t|/r|/n", "");
	}
	/**
	 * des加密
	 * @param str
	 * @param keystr
	 * @return
	 */
	public static String getEncriptCode(String str,String keystr){
		Des d = new Des();
		Key key =d.getKey(keystr.getBytes());
		return d.getEncriptCode(key, str);
	}
	/**
	 * des解密
	 * @param str
	 * @param keystr
	 * @return
	 */
	public static String getDecriptCode(String str,String keystr){
		Des d = new Des();
		Key key =d.getKey(keystr.getBytes());
		return d.getDecriptCode(str, key);
	}
	 
	/**
	 * convertStrToSqlInstr
	 * @param str  a,b,c,d
	 * @return
	 */
	public static String convertStrToSqlInstr(String str,boolean isNumberic){
		StringBuffer sb = new StringBuffer();
		String[] args=str.split(",");
		for (int i = 0; i < args.length; i++) {
			sb.append("'");
			sb.append(args[i]);
			sb.append("'");
			if (i!=args.length-1) {
				sb.append(",");
			}
		}
		String temp=sb.toString();
		if (isNumberic) {
			temp = temp.replace("'", "");
		}
		return temp;
	}
}
